package com.bibrApp.difu.fragment.bucang_record_mvp;

import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bibrApp.difu.R;
import com.bibrApp.difu.base.BaseFragment;

import java.util.List;

import butterknife.BindView;

public class BucangRecordFragment extends BaseFragment<BucangRecordPresenter> implements IBucangRecordView {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    private BucangRecordAdapter adapter;

    int pageNum = 1;
    int pageSize = 10;


    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_list;
    }

    @Override
    protected void initPresenter() {
        presenter = new BucangRecordPresenter(getContext(), this);
        presenter.init();
    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void onDataResult(List<BucangRecordBean> datas, boolean hasNext) {
        if (pageNum == 1 && (datas == null || datas.size() == 0) && adapter.getEmptyViewCount() == 0) {
            adapter.setEmptyView(View.inflate(getContext(), R.layout.empty_order, null));
            return;
        }
        adapter.addData(datas);
        if (!hasNext) {
            adapter.loadMoreEnd();
        } else {
            adapter.loadMoreComplete();
        }
    }

    @Override
    public void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new BucangRecordAdapter();
        recyclerView.setAdapter(adapter);
        adapter.setEnableLoadMore(true);
        adapter.setOnLoadMoreListener(() -> {
            pageNum++;
            presenter.getDatas(pageNum, pageSize);
        }, recyclerView);

        presenter.getDatas(pageNum, pageSize);
    }
}
